package com.hmi.simulatedrive.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.hmi.simulatedrive.activity.MainActivity
import com.hmi.simulatedrive.databinding.FragmentCarControlBinding

class CarControlFragment: BaseFragment() {
    private lateinit var carControlBinding: FragmentCarControlBinding
    private lateinit var viewModel: CarControlFragmentViewModel
    companion object {
        val instance: CarControlFragment by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            CarControlFragment()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel = ViewModelProvider(requireActivity() as MainActivity)[CarControlFragmentViewModel::class.java]
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        carControlBinding = FragmentCarControlBinding.inflate(inflater)
        initView()
        return carControlBinding.root
    }

    /**
     * 初始化界面内容
     * */
    private fun initView() {
        viewModel.carControlLiveData.observe(requireActivity()) {
            // 显示内容
            if (it.lightOff!=carControlBinding.chipLightOff.isChecked) {
                carControlBinding.chipLightOff.isChecked = it.lightOff

            }
            if (carControlBinding.chipLightLeft.isChecked != it.lightLeft) {
                carControlBinding.chipLightLeft.isChecked = it.lightLeft
            }
            if (carControlBinding.chipLightRight.isChecked != it.lightRight){
                carControlBinding.chipLightRight.isChecked = it.lightRight
            }
            if (carControlBinding.chipLightPosition.isChecked != it.lightPosition) {
                carControlBinding.chipLightPosition.isChecked = it.lightPosition
            }
            if (carControlBinding.chipLightFar.isChecked != it.lightFar) {
                carControlBinding.chipLightFar.isChecked = it.lightFar
            }
            if (carControlBinding.chipLightNear.isChecked != it.lightNear) {
                carControlBinding.chipLightNear.isChecked = it.lightNear
            }
            if (carControlBinding.chipLightAbs.isChecked != it.lightAbs) {
            carControlBinding.chipLightAbs.isChecked = it.lightAbs
            }
            if (carControlBinding.chipLightEsc.isChecked != it.lightEsc) {
                carControlBinding.chipLightEsc.isChecked = it.lightEsc
            }
            if (carControlBinding.chipLightHazard.isChecked != it.lightHazard) {
                carControlBinding.chipLightHazard.isChecked = it.lightHazard
            }
            if (carControlBinding.chipLightBrake.isChecked != it.lightBrake) {
                carControlBinding.chipLightBrake.isChecked = it.lightBrake
            }
            if (carControlBinding.chipLightFog.isChecked != it.lightFog) {
                carControlBinding.chipLightFog.isChecked = it.lightFog
            }
            if (carControlBinding.chipDriveModeAd.isChecked != (it.switchADHuman != 0)) {
                carControlBinding.chipDriveModeAd.isChecked = (it.switchADHuman != 0)
            }
            if (carControlBinding.chipDriveModeAcc.isChecked != (it.switchACC != 0)) {
                carControlBinding.chipDriveModeAcc.isChecked = (it.switchACC != 0)
            }
        }

        carControlBinding.tvSettingOk.setOnClickListener(View.OnClickListener { // 用户点击确定
            // 检查IP和端口号是不是符合规定
            parentFragmentManager.popBackStack()
        })

        carControlBinding.chipLightOff.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightOff = carControlBinding.chipLightOff.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightLeft.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightLeft = carControlBinding.chipLightLeft.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightRight.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightRight = carControlBinding.chipLightRight.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightPosition.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightPosition = carControlBinding.chipLightPosition.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightFar.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightFar = carControlBinding.chipLightFar.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightNear.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightNear = carControlBinding.chipLightNear.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightAbs.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightAbs = carControlBinding.chipLightAbs.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightEsc.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightEsc = carControlBinding.chipLightEsc.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightHazard.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightHazard = carControlBinding.chipLightHazard.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightBrake.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightBrake = carControlBinding.chipLightBrake.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipLightFog.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.lightFog = carControlBinding.chipLightFog.isChecked
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipDriveModeAd.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.switchADHuman = if(carControlBinding.chipDriveModeAd.isChecked) 1 else 0
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

        carControlBinding.chipDriveModeAcc.setOnClickListener {
            val hmiControl = viewModel.getHMIPanelBuilder()
            hmiControl.switchACC = if(carControlBinding.chipDriveModeAcc.isChecked) 1 else 0
            (requireActivity() as MainActivity).sendHMIControlMessage(hmiControl.build())
        }

    }
}